/**
 * 
 */
package id.ac.gunadarma.paracorp.repository;

import static org.springframework.data.mongodb.core.query.Criteria.where;
import id.ac.gunadarma.paracorp.model.EnglishSentence;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

/**
 * @author rezalesmana
 *
 */
@Repository
public class EnglishSentenceRepository{
	
	@Autowired
	private MongoTemplate mongoTemplate;
	
	public List<EnglishSentence> findAll(Pageable pageable) {
		List<EnglishSentence> resultList = null;
		
		Query query = new Query(where("id").exists(true)).with(pageable);
		resultList = mongoTemplate.find(query, EnglishSentence.class);
		
		return resultList;
	}
	
	public EnglishSentence findBySentence(String sentence) {
		EnglishSentence englishSentence = null;
		
		Query query = new Query(where("sentence").is(sentence));
		englishSentence = mongoTemplate.findOne(query, EnglishSentence.class);
		
		return englishSentence;
	}
	
	public long getCount() {
		long count = 0;
		
		count = mongoTemplate.getCollection(mongoTemplate.getCollectionName(EnglishSentence.class)).count();
		
		return count ;
	}
	
	public EnglishSentence save(EnglishSentence objectToSave) {
		mongoTemplate.save(objectToSave);
		return objectToSave;
	}
	
	public List<EnglishSentence> save(List<EnglishSentence> objectToSaveList) {
		List<EnglishSentence> resultList = new ArrayList<EnglishSentence>();
		
		for(EnglishSentence objectToSave : objectToSaveList) {
			resultList.add(save(objectToSave));
		}
		
		return resultList;
	}
	
}